#include "UCommunication.h"

UCommunication::UCommunication()
{
    m_comType = UNDEFINE;
    m_flagConnected = false;
}

UCommunication::~UCommunication()
{
    m_comType = UNDEFINE;
    m_flagConnected = false;
}

bool UCommunication::IsConnected()
{
    return m_flagConnected;
}

ComType UCommunication::GetComType()
{
    return m_comType;
}

void UCommunication::SetComType(enum ComType type)
{
    m_comType = type;
}

void UCommunication::SetComParam(enum ComType type, UdpSetStruct udp)
{
    SetComType(type);
    m_myUdp.SetParam(udp);
}

void UCommunication::SetComParam(enum ComType type, TCPSSetStruct tcps)
{
    SetComType(type);
    m_myTCPS.SetParam(tcps);
}

void UCommunication::SetComParam(enum ComType type, TCPCSetStruct tcpc)
{
    SetComType(type);
    m_myClient.SetParam(tcpc);
}
//void UCommunication::SetComParam(SerialStruct serial)
//{
//    m_myPortThread.SetParam(serial);
//}

bool UCommunication::OpenCom()
{

    switch (m_comType)
    {
    case UDPType:
        disconnect(&m_myUdp, 0, 0, 0);
        connect(&m_myUdp, SIGNAL(UdpRecv(const char*,quint64)),
                this, SIGNAL(sigComData(const char*,quint64)));
        m_flagConnected = m_myUdp.OpenUdp();
        break;
    case TCPSType:
        disconnect(&m_myTCPS, 0, 0, 0);
        connect(&m_myTCPS, SIGNAL(sigReceiveMsg(char*,qint64,QString,quint16)),
                this, SLOT(slotRecvData(char*,qint64,QString,quint16)));
        connect(&m_myTCPS, SIGNAL(sigComingConnection(QString,quint16)),
                this , SIGNAL(sigComingConn(QString,quint16)));
        connect(&m_myTCPS, SIGNAL(sigDeleteConnection(QString,quint16)),
                this, SIGNAL(sigDeleteConn(QString,quint16)));
        m_flagConnected = m_myTCPS.OpenTcpS();
        break;
//    case TCPCType:
//        disconnect(&m_myClient, 0, 0, 0);
//        connect(&m_myClient, SIGNAL(sigReceiveMsg(char*,qint64,QString,quint16)),
//                this, SLOT(slotRecvData(char*,qint64,QString,quint16)));
//        connect(&m_myClient, SIGNAL(sigConnectSucess(QString, quint16)),
//                this, SLOT(slotTcpClientConnect(QString, quint16)));
//        connect(&m_myClient, SIGNAL(sigDisConnect()),
//                this, SLOT(slotTcpClientDisconnect()));
//        connect(&m_myClient, SIGNAL(sigConnectError(QString)),
//                this, SIGNAL(sigTcpClientError(QString)));
//        m_myClient.OpenTcpC();
//        break;
    case SERIALType:
//        disconnect(&m_myPortThread, 0, 0, 0);
//        connect(&m_myPortThread, SIGNAL(sigDataRecieve(QByteArray,int)),
//                this, SIGNAL(sigUComData(QByteArray,int)));
//        m_flagConnected = m_myPortThread.OpenCom();
        break;
    default:
        m_flagConnected = false;
    }
    return m_flagConnected;
}

void UCommunication::CloseCom()
{

    switch (m_comType)
    {
    case UDPType:
        m_myUdp.CloseUdp();
        break;
    case TCPSType:
        m_myTCPS.CloseTcpS();
        break;
    case SERIALType:
//        m_myPortThread.CloseCom();
        break;
    case TCPCType:
        m_myClient.CloseTcpC();
    default:
        m_flagConnected = false;
    }
    m_flagConnected = false;
    m_comType = UNDEFINE;
}

int UCommunication::SendData(const char *data, int size, QString address, quint16 port)
{
    if (data == NULL || size < 0)
        return 0;

    if (m_comType == UDPType && m_myUdp.IsConnected())
    {
        return m_myUdp.SendData(data,size,address,port);
    }
    if (m_comType == TCPSType && m_myTCPS.IsConnected())
    {
        return m_myTCPS.SendData(data, size, address, port);
    }
    return 0;
}
int UCommunication::SendData(const char * data, int size)
{
    if (data == NULL || size < 0)
        return 0;

//    if (m_comType == SERIALType && m_myPortThread.IsConnected())
//    {
//        QByteArray byteData(data, size);
//        return m_myPortThread.SendData(byteData, byteData.size());
//    }
    if (m_comType == TCPCType && m_myClient.IsConnected())
    {
        return 0;//m_myClient.SendData(data.toAscii().data(), data.length());
    }
        return 0;
}

void UCommunication::slotTcpClientConnect(QString ip, quint16 port)
{
    m_flagConnected = true;
    emit sigTcpClientConn(ip, port);
}

void UCommunication::slotTcpClientDisconnect()
{
    m_flagConnected = false;
    emit sigTcpClientDisCon();
}

void UCommunication::slotRecvData(char *data, qint64 maxlen, QString host, quint16 port)
{
    emit sigComData(data,maxlen);
}

